package com.indusfo.spc.vo;

import com.indusfo.spc.pojo.Statistical;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: 熊冰
 * @Date: 2020/3/24 13:03
 */
public class DateUtil extends   Throwable {


	Calendar cale = Calendar.getInstance();
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");



	public String getStartWeek(String nowDay) throws ParseException {

		// 获取本周的第一天
		String firstday;
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}

		cale.add(Calendar.WEEK_OF_MONTH, 0);
		cale.set(Calendar.DAY_OF_WEEK, 2);
		firstday = format.format(cale.getTime());
			return firstday;


	}
	public String getEndWeek(String nowDay) throws ParseException{
		// 获取本周的最后一天
		String lastday;
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}
		cale.set(Calendar.DAY_OF_WEEK, cale.getActualMaximum(Calendar.DAY_OF_WEEK));
		cale.add(Calendar.DAY_OF_WEEK, 1);
		lastday = format.format(cale.getTime());
		return lastday;
	}
	public String getStartMonth(String nowDay) throws ParseException{
		// 获取本月的第一天
		String firstday;
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		firstday = format.format(cale.getTime());
		return firstday;
	}
	public String getEndMonth(String nowDay) throws ParseException{
		// 获取本月的最后一天
		String lastday;
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		lastday = format.format(cale.getTime());
		return lastday;
	}
	public  String getStartQuarter(String nowDay)  throws ParseException{
		// 获取本季度的第一天

		LocalDate resDate = LocalDate.now();
		if(nowDay!=null){
			resDate=format.parse(nowDay).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		}
		LocalDate	today = resDate;
		Month month = today.getMonth();
		Month firstMonthOfQuarter = month.firstMonthOfQuarter();
		resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
		return resDate.toString();
	}
	public  String getEndQuarter(String nowDay)  throws ParseException{
		// 获取本季度的最后一天
		LocalDate resDate = LocalDate.now();
		if(nowDay!=null){
			resDate= format.parse(nowDay).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		}
		LocalDate	today = resDate;
		Month month = today.getMonth();
		Month firstMonthOfQuarter = month.firstMonthOfQuarter();
		Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
		resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
		return resDate.toString();
	}
	public String getStartYear(String nowDay) throws ParseException{
		// 获取本年的第一天
		Date date=new Date();
		if(nowDay!=null){
			 date=format.parse(nowDay);
		}
		return new SimpleDateFormat("yyyy").format(date)+"-01-01";
	}
	public String getEndYear(String nowDay) throws ParseException{
		// 获取本年的最后一天
		Date date=new Date();
		if(nowDay!=null){
			date=format.parse(nowDay);
		}
		return new SimpleDateFormat("yyyy").format(date)+"-12-31";
	}
	public String getBeforeOne(String nowDay)throws ParseException{
		// 一天前
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}
		cale.add(Calendar.DATE,-1);
		return format.format(cale.getTime());
	}
	public String getBeforeSeven(String nowDay)throws ParseException{
		// 七天前
		cale = Calendar.getInstance();
		if(nowDay!=null){
			cale.setTime(format.parse(nowDay));
		}
		cale.add(Calendar.DATE,-6);
		return format.format(cale.getTime());
	}
	public String getBeforeThirty(){
		// 三十天前
		cale = Calendar.getInstance();
		cale.add(Calendar.DATE,-29);
		return format.format(cale.getTime());
	}
	public String getBeforeYear(){
		// 去年
		cale = Calendar.getInstance();
		cale.add(Calendar.YEAR,-1);
		return format.format(cale.getTime());
	}
	public String getBeforeTwoYear(){
		// 前两年
		cale = Calendar.getInstance();
		cale.add(Calendar.YEAR,-2);
		return format.format(cale.getTime());
	}
	/**
	 * @Author 熊冰 时间分类  1 当天 2 本周 3 本月 4 本季度 5 本年度 6 当天八点 7 近七天 8 近30天  9 近 一年
	 * @Date 2020/3/23 14:14
	 */
	public Statistical getDate(Statistical statistical)throws ParseException{

		if(statistical.getDateStateId()==null){
			statistical.setDateStateId(2);
		}
		switch (statistical.getDateStateId()) {
			case 1:
				// 1 当天
				statistical.setStartDate(format.format(new Date()));
				statistical.setEndDate(format.format(new Date()));
				break;
			case 2:
				// 2 本周
				statistical.setStartDate(getStartWeek(null));
				statistical.setEndDate(getEndWeek(null));
				break;
			case 3:
				// 3 本月
				statistical.setStartDate(getStartMonth(null));
				statistical.setEndDate(getEndMonth(null));
				break;
			case 4:
				// 4 本季度
				statistical.setStartDate(getStartQuarter(null));
				statistical.setEndDate(getEndQuarter(null));
				break;
			case 5:
				// 5 本年度
				statistical.setStartDate(getStartYear(null));
				statistical.setEndDate(getEndYear(null));
				break;
			default:
				// 9 近一年
				statistical.setStartDate(getBeforeYear());
				statistical.setEndDate(format.format(new Date()));
				break;
		}
		return statistical;
	}
	public Statistical getLastDate(Statistical statistical)throws ParseException{
		if(statistical.getDateStateId()==null){
			statistical.setDateStateId(2);
		}
		switch (statistical.getDateStateId()) {
			case 1:
				// 1 昨天
				statistical.setStartDate(getBeforeOne(format.format(new Date())));
				statistical.setEndDate(getBeforeOne(format.format(new Date())));
				break;
			case 2:
				// 2 上周
				statistical.setStartDate(getBeforeSeven(getBeforeOne(getStartWeek(null))));
				statistical.setEndDate(getBeforeOne(getStartWeek(null)));
				break;
			case 3:
				// 3 上月
				statistical.setStartDate(getStartMonth(getBeforeOne(getStartMonth(null))));
				statistical.setEndDate(getBeforeOne(getStartMonth(null)));
				break;
			case 4:
				// 4 上季度
				statistical.setStartDate(getStartQuarter(getBeforeOne(getStartQuarter(null))));
				statistical.setEndDate(getBeforeOne(getStartQuarter(null)));
				break;
			case 5:
				// 5 上一年度
				statistical.setStartDate(getStartYear(getBeforeOne(getStartYear(null))));
				statistical.setEndDate(getBeforeOne(getStartYear(null)));
				break;
			default:
				// 9 上一年
				statistical.setStartDate(getBeforeTwoYear());
				statistical.setEndDate(getBeforeYear());
				break;
		}
		return statistical;
	}

	public static void main(String[] args) throws ParseException {
		DateUtil t=new DateUtil();
		Statistical statistical=new Statistical();
		statistical.setDateStateId(2);
		statistical=t.getDate(statistical);
		System.out.println(statistical);
		Statistical statistical1=new Statistical();
		statistical1.setDateStateId(2);
		statistical1=t.getLastDate(statistical1);
		System.out.println(statistical1);
//		System.out.println(t.getStartWeek("2019-3-12"));
//		System.out.println(t.getEndWeek("2019-3-12"));
//		System.out.println(t.getStartMonth("2019-3-12"));
//		System.out.println(t.getEndMonth("2019-3-12"));
//		System.out.println(t.getStartYear("2019-3-12"));
//		System.out.println(t.getEndYear("2019-3-12"));
//		System.out.println(t.getStartQuarter("2019-3-12"));
//		System.out.println(t.getEndQuarter("2019-3-12"));




	}

}
